package com.haibei.adapter;

import java.util.ArrayList;
import java.util.HashSet;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.uitl.DateTool;
import com.haibei.R;
import com.haibei.adapter.bean.Article;
import com.haibei.adapter.bean.CollectArticle;
import com.haibei.cache.list.ImageManagerLru;

public class CollectArticleAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<CollectArticle> mArrayListDataList;
	private boolean isEdit = false;
	private HashSet<CollectArticle> mHashSetSelect;

	public CollectArticleAdapter(Activity activity, ArrayList<CollectArticle> datas) {
		this.activity = activity;
		this.mArrayListDataList = datas;
		this.mHashSetSelect = new HashSet<CollectArticle>();
	}

	public void removeSelect() {
		ArrayList<CollectArticle> needDletes=new ArrayList<CollectArticle>();
		for (CollectArticle artice : mArrayListDataList) {
			if (mHashSetSelect.contains(artice)) {
				needDletes.add(artice);
			}
		}
		mArrayListDataList.removeAll(needDletes);
		mHashSetSelect.clear();
		notifyDataSetChanged();
	}
	
	public HashSet<CollectArticle> getSelectSet(){
		return mHashSetSelect;
	}
	@Override
	public int getCount() {
		return mArrayListDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayListDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl;
		if (null == convertView) {
			convertView = View.inflate(activity, R.layout.item_collect_article, null);
			vl = new ViewHolder();
			vl.textViewTitle = (TextView) convertView.findViewById(R.id.textViewTitle);
			vl.textViewContent = (TextView) convertView.findViewById(R.id.textViewContent);
			vl.timeAndAuthor = (TextView) convertView.findViewById(R.id.textViewTimeAndAuthor);
			vl.imageViewUrl = (ImageView) convertView.findViewById(R.id.imageViewImg);
			vl.relativeLayoutRadio = convertView.findViewById(R.id.relativeLayoutRadio);
			vl.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxDel);
			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		final CollectArticle consult = mArrayListDataList.get(position);
		vl.textViewTitle.setText(consult.getTitle());
		vl.textViewContent.setText(consult.getShortContent());
		long time = Long.parseLong(consult.getPublishTime());
		// =========
		vl.timeAndAuthor.setText(DateTool.getLongTime4Ymdhms(time) + " " + consult.getAuthor());
		if (isEdit) {
			vl.relativeLayoutRadio.setVisibility(View.VISIBLE);
			vl.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						mHashSetSelect.add(consult);
					} else {
						mHashSetSelect.remove(consult);
					}
				}
			});
			if (mHashSetSelect.contains(consult)) {
				vl.checkBox.setChecked(true);
			}
		} else {
			vl.relativeLayoutRadio.setVisibility(View.GONE);
			vl.checkBox.setOnCheckedChangeListener(null);
		}

		// SimpleImageLoader.display(vl.imageViewUrl,
		// consult.getCoverImg(),R.drawable.bg_default_consult);
		ImageManagerLru.from(activity).displayImage(vl.imageViewUrl, consult.getCoverImg(), R.drawable.bg_default_consult);
		return convertView;
	}

	public void setEditEable(boolean isEdit) {
		this.isEdit = isEdit;
		notifyDataSetChanged();
	}

	public boolean isEdit() {
		return isEdit;
	}

	class ViewHolder {
		TextView textViewTitle;
		TextView textViewContent;
		TextView timeAndAuthor;
		ImageView imageViewUrl;
		View relativeLayoutRadio;
		CheckBox checkBox;
	}

}
